package com.distribution.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.distribution.system.domain.SysNotice;
import com.distribution.system.mapper.SysNoticeMapper;
import com.distribution.system.service.ISysNoticeService;
import com.distribution.system.service.NoticeUserManageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService
{
    @Resource
    private SysNoticeMapper noticeMapper;
    @Resource
    private NoticeUserManageService noticeUserManageService;

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @PreAuthorize("@ss.hasAnyRoles('operationSupervisor')")
    @Transactional
    @Override
    public int insertNotice(SysNotice notice)
    {
        notice.setCreateTime(new Date());
        notice.setUpdateBy(notice.getCreateBy());
        notice.setUpdateTime(new Date());
        int flag = noticeMapper.insertNotice(notice);
        System.out.println("notice = " + notice);
        if ("2".equals(notice.getNoticeType())) return flag;
        return flag <= 0 ? flag : noticeUserManageService.addNoticeToAllAgent(notice);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeId 需要删除的公告ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        System.out.println("noticeId = " + noticeId);
        SysNotice sysNotice = noticeMapper.selectNoticeById(noticeId);
        int flag = noticeMapper.deleteNoticeById(noticeId);
        System.out.println("sysNotice = " + sysNotice);
        if ("2".equals(sysNotice.getNoticeType())) return flag;
        return flag <= 0 ? flag : noticeUserManageService.delNoticeUserManageById(noticeId);
    }

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        return noticeMapper.updateNotice(notice);
    }

    /**
    * @Author: GodLu
    * @Date: 2024/7/11 9:13
    * @Description: 获取最近的公告
    * @return: SysNotice
    */
    @Override
    public SysNotice selectLatestNotice() {
        return noticeMapper.selectLatestNotice();
    }
}
